package org.txazo.blog.commons.xml;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.txazo.blog.util.StringUtil;

/**
 * XmlConverter
 * 
 * @author Tu XiaoZhou
 * @since 2013-09-03
 */
public class XmlConverter {

	private boolean firstFlag = true;
	private boolean secondFlag = false;
	private boolean tabFlag = false;
	private boolean tabText = false;
	private int listCount = 0;
	private int codeCount = 0;
	private boolean preTextStart = true;
	private boolean preTextEnd = false;

	public void converterToXml(String input, String output) {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(input)));
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(output)))) {
			bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			bw.newLine();
			bw.newLine();
			bw.write("<div id=\"nav\">");
			bw.newLine();

			String temp = null;
			String trim = null;
			while ((temp = br.readLine()) != null) {
				trim = temp.trim();
				System.out.println(trim);
				if (StringUtil.isEmpty(trim)) {
					bw.write(getBlank(2) + "</pre>");
					bw.newLine();
					preTextStart = true;
					preTextEnd = false;
				} else {
					if (firstFlag) {
						bw.write(getBlank(1) + "<div id=\"nav-top\">");
						bw.newLine();
						bw.write(getBlank(2) + "<a name=\"title-0\">");
						bw.newLine();
						bw.write(getBlank(3) + "<h3>" + trim + "</h3>");
						bw.newLine();
						bw.write(getBlank(2) + "</a>");
						bw.newLine();
						bw.write(getBlank(1) + "</div>");
						bw.newLine();

						firstFlag = false;
					} else if (trim.equals("<code type=\"list\" />")) {
						if (preTextEnd) {
							bw.write(getBlank(2) + "</pre>");
							bw.newLine();
							preTextStart = true;
							preTextEnd = false;
						}
						if (listCount > 0) {
							bw.write(getBlank(1) + "</div>");
							bw.newLine();
						}
						bw.write(getBlank(1) + "<div class=\"nav-list\">");
						bw.newLine();
						listCount++;
						secondFlag = true;
						tabText = false;
					} else if (secondFlag) {
						bw.write(getBlank(2) + "<div class=\"nav-title\">");
						bw.newLine();
						bw.write(getBlank(3) + "<a name=\"title-1\">");
						bw.newLine();
						bw.write(getBlank(4) + "<span class=\"nav-title\">"
								+ trim + "</span>");
						bw.newLine();
						bw.write(getBlank(3) + "</a>");
						bw.newLine();
						bw.write(getBlank(2) + "</div>");
						bw.newLine();

						secondFlag = false;
					} else if (trim.equals("<code type=\"tab\">")) {
						if (preTextEnd) {
							bw.write(getBlank(2) + "</pre>");
							bw.newLine();
							preTextStart = true;
							preTextEnd = false;
						}
						if (codeCount > 0) {
							bw.write(getBlank(2) + "</div>");
							bw.newLine();
						}
						bw.write(getBlank(2) + "<pre class=\"nav-tab\">");
						bw.newLine();
						tabFlag = true;
						tabText = false;
					} else if (tabFlag) {
						bw.write(getBlank(3) + "<span class=\"tab-title\">"
								+ trim + "</span>");
						bw.newLine();
						bw.write(getBlank(3) + "<hr class=\"tab-sep\" />");
						bw.newLine();
						tabFlag = false;
						tabText = true;
					} else if (trim.startsWith("<code type=")) {
						if (preTextEnd) {
							bw.write(getBlank(2) + "</pre>");
							bw.newLine();
							preTextStart = true;
							preTextEnd = false;
							tabText = false;
						}
						bw.write(getBlank(2) + "<pre class=\"nav-"
								+ trim.substring(12, trim.length()));
						bw.newLine();
					} else if (trim.equals("</code>")) {
						bw.write(getBlank(2) + "</pre>");
						bw.newLine();
					} else {
						if (!tabText && preTextStart) {
							bw.write(getBlank(2) + "<pre class=\"nav-text\">");
							bw.newLine();
							preTextStart = false;
							preTextEnd = true;
						}
						bw.write(trim);
						bw.newLine();
					}
				}
			}
			if (listCount > 0) {
				bw.write(getBlank(1) + "</div>");
				bw.newLine();
			}
			bw.write("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String getBlank(int count) {
		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < count; i++) {
			sb.append("    ");
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		new XmlConverter()
				.converterToXml(
						"F:/GraduationProject/txazo/BlogSite/src/resource/blog-model.html",
						"F:/1.xml");
	}

}
